#!/usr/bin/env bash

localpath="/home/ec2-user/workspace/nacos/*.config"

echo "write local path is "$localpath
for config_file in $localpath; do
        nacos_file=`echo $config_file|sed "s/.config//g"`
        config=`cat $config_file`
        # echo "config file is "$config_file
        # echo "nacos file is "$nacos_file
        echo "------"
        # dataId也是filename
        dataId=`echo $config|jq -r ".dataId"`
        group=`echo $config|jq -r ".group"`
        md5=`echo $config|jq -r ".md5"`
        tenant=`echo $config|jq -r ".tenant"`
        appName=`echo $config|jq -r ".appName"`
        type=`echo $config|jq -r ".type"`
        # 去除首尾双引号，URL encodeuricomponent编码
        content=`cat $nacos_file| sed -e 's/^"//' -e 's/"$//'| xxd -p | tr -d '\n' | sed 's/\(..\)/%\1/g'`
        request_body="dataId=$dataId&group=$group&content=$content&config_tags=&type=$type&appName=$appName&tenant=$tenant&namespaceId=$tenant"
        # echo "request body is $request_body"

        result=`curl -X POST 'http://64.227.100.83:30001/nacos/v1/cs/configs'  -d  $request_body`
        echo "$dataId upload result is :$result"
done